//
//  HYBDpanListView.m
//  XPlayer
//
//  Created by 张水 on 2023/9/21.
//

#import "HYBDpanListView.h"

#import "HYBDPanFileListCell.h"

@interface HYBDpanListView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;


@end

@implementation HYBDpanListView

- (instancetype)initWithViewModel:(HYBDPanFileListViewModel *)viewModel fileType:(BDPanFileType)bdFiletype
{
    self = [super init];
    if(self){
        self.viewModel = viewModel;
        self.bdFiletype = bdFiletype;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsInSection:section withFileType:self.bdFiletype];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYBDPanFileListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLIDHYBDPanFileListCell" forIndexPath:indexPath];
    HYBDPanFileModelList *listData = [self.viewModel cellForRowAtIndexPath:indexPath withFileType:self.bdFiletype];
    [cell bindViewModel:listData];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kItemListHeight+20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectRowAtIndexPath:indexPath withFileType:self.bdFiletype];
}

#pragma mark getter
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#F5F5F5" darkModeHex:@"#181A22"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.contentInset = UIEdgeInsetsMake(2.5, 0, 0, 0);
        [_tableView registerClass:[HYBDPanFileListCell class] forCellReuseIdentifier:@"CELLIDHYBDPanFileListCell"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

@end
